package com.chinaunicom.emergency.service;

import com.chinaunicom.emergency.exception.OAuthException;
import com.chinaunicom.emergency.model.UserRole;
import com.chinaunicom.emergency.model.YjglUser;
import com.chinaunicom.emergency.repository.UserRoleRepository;
import com.chinaunicom.emergency.repository.UsersRepository;
import com.chinaunicom.emergency.util.JPAUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author liliang
 * @Date 2019/12/17 15:37
 * @Version 1.0
 */
@Service
@Log4j2
public class UserService {
    @Autowired
    private UsersRepository usersRepository;
    @Autowired
    UserRoleRepository userRoleRepository;

    public Specification findPageByCondition(Map para){
        return new Specification<YjglUser>() {

            @Override
            public Predicate toPredicate(Root<YjglUser> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();

                JPAUtil.like(list,cb,root,"userName",para);
                JPAUtil.like(list,cb,root,"realName",para);
                JPAUtil.equal(list,cb,root,"isSoftDel",para);
                if(para.containsKey("orgIdList")){
                    JPAUtil.in(list,cb,root,"orgId",para,"orgIdList");
                }

                return cb.and(list.toArray(new Predicate[list.size()]));
            }
        };
    }

    @Transactional(rollbackFor = Exception.class)
    public void setRole(String userId, List<String> roleList, String currUserId) throws OAuthException {
        List<UserRole> list = new ArrayList();
        userRoleRepository.deleteByUserId(userId);
        for(String roleId : roleList){
            UserRole userRole = new UserRole();
            userRole.setCreateBy(currUserId);
            userRole.setCreateTime(new Date());
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRole.setIsSoftDel(0);
            list.add(userRole);
        }
        userRoleRepository.saveAll(list);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteUserToken(String userId) throws OAuthException {
//        authClient.revokeToken(null,null,userId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(YjglUser user) {
        usersRepository.save(user);
        userRoleRepository.deleteByUserId(user.getPid());
    }


    /**
     * a == ? and (b like ? or c like ?)
     * 复杂sql写法
     * @param para
     * @return
     */
    public Specification<YjglUser> getYjglUserByCondition(Map para){
        return  new Specification<YjglUser>() {
            @Override
            public Predicate toPredicate(Root<YjglUser> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                //a == ? and (b like ? or c like ?)
                List<Predicate> list = new ArrayList<>();
                Predicate p1 = criteriaBuilder.equal(root.get("isSoftDel"),para.get("isSoftDel"));
                Predicate p2 = criteriaBuilder.like(root.get("userName"),"%"+para.get("userName")+"%");
                Predicate p3 = criteriaBuilder.or(criteriaBuilder.like(root.get("realName"),"%"+para.get("realName")+"%"),p2);
                list.add(p1);
                list.add(p3);
                return criteriaBuilder.and(list.toArray(new Predicate[0]));
            }
        };
    }
}
